/**
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: PropertiesUtils.java 1211 2010-09-10 16:20:45Z calvinxiu $
 */
package com.surfilter.mq.util.properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Properties Util函数.
 * 
 * @author calvin
 */
public class PropertiesUtils {

	private static final String DEFAULT_ENCODING = "UTF-8";

	private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
	
	public static String getStringValue(String key) {
		String value = "";
		try {
			value = CustomizedPropertyConfigurer.getContextProperty(key).toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 增加默认的配置
	 * @param key
	 * @param defaultString
	 * @return
	 */
	public static String getStringValue(String key,String defaultString) {
		String value = null;
		try {
			Object v = CustomizedPropertyConfigurer.getContextProperty(key);
			if(v == null)
				value = defaultString;
			else 
				value = v.toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key+",返回默认值:"+defaultString);
			return defaultString; 
		}
		return value;
	}
	
	public static boolean getBooleanValue(String key) {
		Boolean value = null ;
		try {
			value = new Boolean(CustomizedPropertyConfigurer.getContextProperty(key).toString());
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
			e.printStackTrace();
		}
		return value;
	}
	public static boolean getBooleanValue(String key,boolean defaultBoolean) {
		Boolean value = null ;
		try {
			value = new Boolean(CustomizedPropertyConfigurer.getContextProperty(key).toString());
		} catch (Exception e) {
			logger.error("找不到配置项："+key+",返回默认值:"+defaultBoolean);
			return defaultBoolean;
		}
		return value;
	}
	
	public static int getIntValue(String key) {
		String value = "";
		int result = 0;
		try {
			value = CustomizedPropertyConfigurer.getContextProperty(key).toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
			e.printStackTrace();
		}
		try {
			result = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			logger.error("不能转换数据："+value);
			e.printStackTrace();
		}
		return result;
	}
	public static int getIntValue(String key,int defaultInt) {
		String value = "";
		int result = 0;
		try {
			value = CustomizedPropertyConfigurer.getContextProperty(key).toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
		}
		try {
			result = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			logger.error("不能转换数据："+value+",返回默认值:"+defaultInt);
			return defaultInt;
		}
		return result;
	}
	
	/**
	 * 这里主要是存放解决文件统计图的文件类型匹配
	 * 也可以通用map,
	 * 配置要求如下 [key,value][key,value]
	 * @param key
	 * @param regex "\\[(\\w+\\,\\w+)\\]"
	 * @param separator 分隔符
	 * @return
	 */
	public static Map<String,String> getMapProperties(String key,String regex,String separator){
		String source = getStringValue(key);
		if(separator==null){
			separator =",";
		}
		Map<String,String>  typesMap = new HashMap<String,String>();
		if(source!=null && source.length()>0){
			Matcher ma = Pattern.compile(regex).matcher(source);
			while(ma.find()){
				try{
					String matchs = ma.group(1);
					String[] splitS= matchs.split(separator);
					System.out.println("key:"+splitS[0]+",value:"+splitS[1]);
					typesMap.put(splitS[0], splitS[1]);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return typesMap;
	}
	
	public static double getDoubleValue(String key) {
		String value = "";
		double result = 0;
		try {
			value = CustomizedPropertyConfigurer.getContextProperty(key).toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
			e.printStackTrace();
		}
		try {
			result = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			logger.error("不能转换数据："+value);
			e.printStackTrace();
		}
		return result;
	}
	
	public static double getDoubleValue(String key,double defaultInt) {
		String value = "";
		double result = 0;
		try {
			value = CustomizedPropertyConfigurer.getContextProperty(key).toString();
		} catch (Exception e) {
			logger.error("找不到配置项："+key);
		}
		try {
			result = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			logger.error("不能转换数据："+value+",返回默认值:"+defaultInt);
			return defaultInt;
		}
		return result;
	}
	
	
	
	public static void main(String[] args) {
		getMapProperties("dic.file.type","\\[(\\w+\\,\\w+)\\]",",");
	}
}
